#ifndef MODBUS_CONFIG_H
#define MODBUS_CONFIG_H

#include <modbus/modbus.h>

#define modbus_config_file "modbus"

#define SECTION_MASTER1 "master1"
#define SECTION_MASTER2 "master2"
#define SECTION_SLAVE1 "slave1"
#define SECTION_SLAVE2 "slave2"
#define SECTION_UART2 "uart2"
#define SECTION_UART3 "uart3"
#define SECTION_FAN1 "fan1"
#define SECTION_FAN2 "fan2"
#define SECTION_ISO "iso"

#define ENABLE "enable"
#define MASTER_IP "request_server_ip"
#define PORT "port"
#define PERIOD "period"
#define REQUEST_SLAVE_ID "request_slave_id"
#define SLAVE_IP "server_ip"
#define UART_BAUD "baud"
#define UART_DATA_BIT "data_bit"
#define UART_PARITY "parity"
#define UART_STOP_BIT "stop_bit"
#define UART_SLAVE_ID "slave_id"
#define UART_MODE "mode"
#define UART_FAN "fan"
#define UART_INSULAT "insulat"


typedef struct 
{
    int sid;                    /* 从机地址 */
    int fc;                     /* 功能码 */
    int addr;                  /* 起始地址 */
    int num;                   /* 读寄存器数量 */
    uint16_t val;                   /* 寄存器值 */
} Modbus_info_t;


// 表示每个接口的选项数据结构
typedef struct
{
    int port;                            // 端口
    int period;                          // 周期
    int fanNum;                          // 风扇
    int insulation;                      // 绝缘
    int stop_bit;                        // 停止位
    int data_bit;                        // 数据位
    int baud;                            // 波特率
    int slave_id;                        // 从机ID
    int request_slave_id;                // 设备地址
    char parity;                         // 奇偶校验
    char enable[5];                      // 接口使能
    char mode[10];                       // 服务器使能
    char request_server_ip[16];          // 被请求的server IP地址
    char server_ip[16];                  // server IP地址
} InterfaceOptions;

typedef struct
{
    InterfaceOptions master1;
    InterfaceOptions master2;
    InterfaceOptions slave1;
    InterfaceOptions slave2;
    InterfaceOptions uart2;
    InterfaceOptions uart3;
    InterfaceOptions fan1;
    InterfaceOptions fan2;
    InterfaceOptions iso;
} ConfigData;


// 解析配置文件，并将结果存储到全局静态变量中
int parse_config_file();

// 获取配置文件解析结果的单例对象
const ConfigData *get_config_data_instance();

#endif